<?php
class Admin_SmtpController extends Zend_Controller_Action
{
    protected $_config_smtp;
    
    public function init ()
    {
        // Models
        $this->_config_smtp = new Application_Model_ConfigSmtp();
        
    }
    public function preDispatch ()
    {
        if (! Zend_Auth::getInstance()->getIdentity()) {
            $this->_redirect('/autenticacao');
        } else {
            $auth = Zend_Auth::getInstance();
            $perfil = $auth->getIdentity()->idperfil;
            if ($perfil == 2) {
                $this->_redirect('/admin/');
            }
        }
    }
    public function minhasContasSmtpAction()
    {
        $pagina = (int) ($this->_getParam('pagina', 1));
        $data = $this->_config_smtp->listaAllContasSmtp(); 
        $paginacao = Zend_Paginator::factory($data);
        $paginacao->setItemCountPerPage(10);
        $paginacao->setCurrentPageNumber($pagina);
        $this->view->listaContasSmtpPaginacao = $paginacao;
        $this->view->teste = $data->toArray();
    }
    
    public function editarContaAction ()
    {    	
	    $rlista = $this->_config_smtp->procuraContaSmtp($this->_request->getParam('id'));
	    $form = new Application_Form_Smtp();
	    $form->getElement('idconfig')->setValue($rlista['idconfig_smtp']);
	    $form->populate($rlista);
	    $this->view->form = $form;
	    //Se for via post
	    if($this->_request->isPost())
		{
			$data = $this->_request->getPost();	
			if ($form->isValid($data)) {
		    		    	
		    	$this->_config_smtp->update($data['idconfig'], $data['smtp'], $data['conta'], $data['ssl'], $data['senha'], $data['porta'], $data['envioPorHora']);
		    	$this->_redirect('/admin/smtp/minhas-contas-smtp');
			}
		 }
    }
    
    public function criarContaAction ()
    {    	

	    $form = new Application_Form_Smtp();
	    $this->view->form = $form;
	    if($this->_request->isPost())
		{
			$data = $this->_request->getPost();	
			if ($form->isValid($data)) {	    	
		    	$this->_config_smtp->save($data['smtp'], $data['conta'], $data['ssl'], $data['senha'], $data['porta'], $data['envioPorHora']);
		    	$this->_redirect('/admin/smtp/minhas-contas-smtp');
			}
		 }
    }
    public function ativarSmtpAction ()
    {    	
    	 if ($this->_hasParam('id') == false) {
            $this->_redirect('/admin/smtp/minhas-contas-smtp');
         } else {
			$idconfig_smtp = $this->_request->getParam('id');	    	
			$this->_config_smtp->ativar($idconfig_smtp);
			$this->_redirect('/admin/smtp/minhas-contas-smtp');
		}
    }
    public function removeSmtpAction()
    {
   		 if ($this->_hasParam('id') == false) {
            $this->_redirect('/admin/smtp/minhas-contas-smtp');
         } else {
			$idConfig = $this->_request->getParam('id');	    	
			$this->_config_smtp->remove($idConfig);
			$this->_redirect('/admin/smtp/minhas-contas-smtp');
		 }	
    }
    
}